const { Controller } = require("egg")

class UserController extends Controller {
    //增加用户
    async create() {
            try {
                const body = this.ctx.request.body
                await this.ctx.service.user.addUser(body)
                this.ctx.body = {
                    code: 20000,
                    message: true
                }
            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false
                }
            }
        }
        //获取用户
    async index() {
            try {
                const User = await this.ctx.service.user.getUser()
                this.ctx.body = {
                    code: 20000,
                    message: true,
                    data: User
                }
            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false
                }
            }
        }
        //删除用户
    async destroy() {
            try {
                const id = this.ctx.params.id
                await this.ctx.service.user.deleteUser(id)
                this.ctx.body = {
                    code: 20000,
                    message: true
                }
            } catch (error) {
                this.ctx.body = {
                    code: 50000,
                    message: false
                }
            }
        }
        //更新用户
    async update() {
        try {
            const id = this.ctx.params.id
            const body = this.ctx.request.body
            await this.ctx.service.user.updateUser(id, body)
            this.ctx.body = {
                code: 20000,
                message: true,
            }
        } catch (error) {
            this.ctx.body = {
                code: 10000,
                message: false
            }
        }
    }
}

module.exports = UserController